package livesun.justiceeternal.activity;

import android.text.format.Formatter;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import livesun.justiceeternal.R;
import livesun.justiceeternal.base.BaseActivity;
import livesun.justiceeternal.utils.CommonUtil;
import livesun.justiceeternal.utils.ToastUtil;
import livesun.justiceeternal.view.progressbar.NumberProgressBar;

/**
 * 类描述：我的下载
 * 创建人：livesun
 * 创建时间：2018/1/4.
 * 修改人：
 * 修改时间：
 * github：https://github.com/livesun
 */
public class OffLineDownloadActivity extends BaseActivity{

    private NumberProgressBar mProgressBar;
    private TextView mCacheSize;
    private ImageView mRefresh;

    @Override
    public int getResourcesId() {
        return R.layout.activity_offline_download;
    }

    @Override
    public void initView() {
        mDefultTitleBar.setTitle("我的下载");
        mProgressBar=findView(R.id.progress_bar);
        mCacheSize=findView(R.id.cache_size_text);
        mRefresh = findView(R.id.download_refresh);
        long phoneTotalSize = CommonUtil.getPhoneTotalSize();
        long phoneAvailableSize = CommonUtil.getPhoneAvailableSize();
        //转换为G的显示单位
        String totalSizeStr = Formatter.formatFileSize(this, phoneTotalSize);
        String availabSizeStr = Formatter.formatFileSize(this, phoneAvailableSize);
        //计算占用空间的百分比
        int progress = countProgress(phoneTotalSize, phoneAvailableSize);
        mProgressBar.setProgress(progress);
        mCacheSize.setText("主存储:" + totalSizeStr + "/" + "可用:" + availabSizeStr);
    }

    @Override
    public void initListener() {
        mRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showShort("刷新");
            }
        });
    }

    @Override
    public void onClick(View v, int id) {

    }

    private int countProgress(long phoneTotalSize, long phoneAvailableSize) {
        double totalSize = phoneTotalSize / (1024 * 3);
        double availabSize = phoneAvailableSize / (1024 * 3);
        //取整相减
        int size = (int) (Math.floor(totalSize) - Math.floor(availabSize));
        double v = (size / Math.floor(totalSize)) * 100;
        return (int) Math.floor(v);
    }
}
